﻿using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Reflection;
using System.Text;
using BenCaoXuan.Infrastructure.Attr;
using BenCaoXuan.Api.Admin.Filters;
using BenCaoXuan.Service.Interface.admin;

namespace BenCaoXuan.Api.Admin.Filters
{
    /// <summary>
    /// 操作日志过滤器
    /// </summary>
    public class Action_Filter : Base_Filter
    {
        //private readonly I_Log_Service _log_Service;

        //public Action_Filter(I_Login_Service login_Service,I_Log_Service log_Service) : base(login_Service)
        //{
        //    _log_Service = log_Service;

        //}

        //public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        //{
        //    //添加日志
        //    var action = context.ActionDescriptor as ControllerActionDescriptor;
        //    var attr = action!.MethodInfo.GetCustomAttribute<Action_Attribute>();//获取日志特性
        //    if (attr == null)
        //    {
        //        //没有该属性 则无需记录日志
        //        await base.OnResultExecutionAsync(context, next);
        //        return;
        //    }

        //    string paras = "";
        //    string method = context.HttpContext.Request.Method.ToUpper();
        //    if (context.HttpContext.Request.Method.ToUpper() == "GET")
        //    {
        //        var keys = context.HttpContext.Request.Query.Keys;
        //        foreach (var item in keys)
        //        {
        //            paras += $"{item}={context.HttpContext.Request.Query[item]};";
        //        }

        //    }
        //    else
        //    {
        //        var request = context.HttpContext.Request;
        //        request.EnableBuffering();
        //        if (request.Method.ToLower().Equals("post"))
        //        {
        //            request.Body.Seek(0, SeekOrigin.Begin);
        //            using var reader = new StreamReader(request.Body, Encoding.UTF8);
        //            paras = await reader.ReadToEndAsync();
        //        }

        //    }
        //    paras = paras.Length > 999 ? paras.Substring(0, 999) : paras;

        //    string url = context.HttpContext.Request.Path;
        //    var code = context.HttpContext.User.Identity?.Name;//NAME=Code 

        //    //获取当前登录的用户
        //    var user = Get_Users(code);
        //    //通过属性来获取
        //    _log_Service.AddLog(new Infrastructure.EFCore.MySql.SysActionLog()
        //    {
        //        AlId=Config.GUID(),
        //        AlAction=attr.Action,
        //        AlCreateTime=DateTime.Now,
        //        AlDesc=null,
        //        AlGroup=attr.Group,
        //        AlIp=Config.GetIp(),
        //        AlMethod=method,
        //        AlParams=paras,
        //        AlUId=user.id,
        //        AlUrl=url
        //    });


        //    await base.OnResultExecutionAsync(context, next);
        //}
        public Action_Filter(I_Login_Service login_Service) : base(login_Service)
        {
        }
    }
}
